<!DOCTYPE html>
<html lang="en">

	<head>
		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>深拷贝一个对象，假设对象有三层。（找递归函数）</title>
	</head>
	<body>
	</body>
	<script>
		let obj = {
			uname: '琑',
			age: 18,
			sex: '男',
			color: ['red', 'blue', 'green', 'yellow'],
			message: {
				index: 10,
				score: 95,
				look: {
					one: 1,
					two: 2,
					three: {
						four: 5
					}
				}
			}
		}
		let newObj = {}
		function kaobei(newObj, obj) {
			for(let key in obj) {
				//obj[key]是数组
				if(obj[key] instanceof Array) { 
					newObj[key] = []
					//假如数组里嵌套数组，用递归实现深拷贝
					kaobei(newObj[key], obj[key])
				} else if(obj[key] instanceof Object) {
					newObj[key] = {}
					//假如对象里嵌套对象，用递归实现深拷贝
					kaobei(newObj[key], obj[key])
				} else {
					newObj[key] = obj[key]
				}
			}
		}
		kaobei(newObj, obj)
		obj.message.score = 100
		console.log(obj, newObj)
	</script>
</html>